import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import "package:get/get.dart";
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';

RxMap info = {}.obs;
RxMap auth = {}.obs;

class PropertyLIst extends StatefulWidget {
  PropertyLIst({super.key});
  @override
  State<PropertyLIst> createState() => _PropertyLIst();
}

 getuser(){
   Get.find<HTTPS>().nogetData('/bbs/userInfo', {}).then((res) {
        info.value = res;
        // Get.find<MyMethod>().myset('bbsuser', res);
      });
    }
    
class _PropertyLIst extends State<PropertyLIst> {

   


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
     
      getuser();
    


      Get.find<HTTPS>().nogetData('/bbs/identity', {}).then((res){
          if(res!=null){
            auth.value=res;
          }
      });

    });
  }

  @override
  Widget build(BuildContext context) {
    final Height = MediaQuery.of(context).padding.top+15;
    final width = MediaQuery.of(context).size.width / 4.4;

    return Padding(
      padding: EdgeInsets.only(right: 15, left: 15),
      child: ListView(
        children: [
          SizedBox(height: Height),
          // Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          //   SizedBox(),
          //   TextButton(
          //     onPressed: () => {

          //        Get.dialog(
          //       AlertDialog(
          //         backgroundColor: Colors.transparent,
          //         content: Stack(
          //           children: [
          //             ClipRRect(
          //               borderRadius: BorderRadius.circular(15.0),
          //               child: Image.asset(
          //                 'static/qidai.png',
          //                 width: 250,
          //                 fit: BoxFit.fitWidth,
          //               ),
          //             ),
          //           ],
          //         ),
          //       ),
          //     )
           

          //     },
          //     child: Image.asset('static/images/sao.png', width: 20),
          //   )
          // ]),
          // Row(
          //   children: [
          //     Image.asset('static/images/denglu.png', width: 50),
          //     Text(
          //       '点击登录',
          //       style: TextStyle(fontSize: 16),
          //     )
          //   ],
          // ),

          loginInfo(),

          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              GestureDetector(
                onTap: (){
                  Get.toNamed('/osg_authentication');
                },
                child: Container(
                margin: EdgeInsets.only(top: 15),
                decoration: BoxDecoration(
                    color: Color(0xffE4F1FE),
                    borderRadius: BorderRadius.circular(10)),
                padding:
                    EdgeInsets.only(left: 15, right: 15, top: 5, bottom: 5),
                child: 
                Obx(()=>

                  auth['status']==1&& auth['type']==1||auth['status']==1&&auth['type']==2?
                  Row(
                    children: [
                      Image.asset( auth['type'] ==2?'static/images/zuzhi.png':'static/images/xingqu.png', width: 15),
                      // Image.asset('static/images/xingqu.png', width: 10),
                      SizedBox(width: 5),
                      Text(
                        '${auth['title']}',
                        style: TextStyle(fontSize: 14.5),
                      )
                    ],
                  ):Row(
                    children: [
                      // Image.asset('static/images/zuzhi.png', width: 15),
                      // Image.asset('static/images/xingqu.png', width: 15),
                      //SizedBox(width: 5),
                      Text(
                        '获得OSG官方认证领取专属身份标识',
                        style: TextStyle(fontSize: 14.5),
                      )
                    ],
                  )

                )
                ,
              ),
              )
              ,
            ],
          ),

          SizedBox(height: 20),
          userTopInfo(),
          Divider(
            color: Color(0xffdddddd),
            thickness: 0.5,
            height: 40,
          ),
          SizedBox(height: 5),
          Text(
            '常用功能',
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 10),
          functionality(width: width),
          Divider(
            color: Color(0xffdddddd),
            thickness: 0.5,
            height: 40,
          ),
          SizedBox(height: 5),
          Text(
            '任务中心',
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 10),
          Task(width: width),
          Divider(
            color: Color(0xffdddddd),
            thickness: 0.5,
            height: 40,
          ),
          SizedBox(height: 5),
          Text(
            '其它',
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 10),
          Other(width: width),
        ],
      ),
    );
  }
}

class loginInfo extends StatelessWidget {
  const loginInfo({super.key});

  

  @override
  Widget build(BuildContext context) {

      String? qian='';
      String? hou='';
      final metaid = Get.find<MyMethod>().myget('user')['metaAddress'];
      if(metaid.isNotEmpty){
           qian = metaid.substring(0,5);
           hou = metaid.substring(metaid.length - 4);
      }
  
     
    Future<void> copyToClipboard(BuildContext context,textToCopy) async {
    await Clipboard.setData(ClipboardData(text: textToCopy));

    Get.find<HTTPS>().showTost('已复制到剪贴板');

  }


    return Obx(() => GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => {Get.toNamed('/users/user/index')},
        child: Row(
          children: [
            Stack(
              alignment: Alignment.center,
              children: [
                CircleAvatar( 
                    radius: 30,
                    backgroundImage:NetworkImage('${info['headimg']}')
                  )
                ,
              ],
            ),
            SizedBox(width: 10), // 图片和文字之间的间距

            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 5,),
                Text(
                  '${info['name']}',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                // SizedBox(height: 5), // 公司名称和UID之间的间距
                Text(
                  '${info['uuid']}',
                  style: TextStyle(
                    color: Colors.grey,
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    copyToClipboard(context,metaid);
                  },
                  child: Row(
                  children: [
                    Text('区块链地址:$qian...$hou',
                  style: TextStyle(
                      fontSize: 12,
                    color: Colors.grey,
                  ),),
                  SizedBox(width: 5,),
                   Icon(Icons.copy,size:13)
                  ],
                ),
                )
                
                // IconButton(icon: Icon(Icons.copy),
                // color: Colors.grey,
                // iconSize:10,
                //  onPressed: () async {

                //  }
                // )

              ],
            ),
            Expanded(child: Container()),
            TextButton(
              onPressed: () => {

                 Get.dialog(
                AlertDialog(
                  backgroundColor: Colors.transparent,
                  content: Stack(
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(15.0),
                        child: Image.asset(
                          'static/qidai.png',
                          width: 250,
                          fit: BoxFit.fitWidth,
                        ),
                      ),
                    ],
                  ),
                ),
              )
           

              },
              child: Image.asset('static/images/sao.png', width: 20),
            ),
            Icon(
              Icons.arrow_forward_ios,
              size: 16,
            ),
          ],
        )));
  }
}

class Other extends StatelessWidget {
  final double width;
  const Other({super.key, required this.width});
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Wrap(
        runSpacing:15,
        children: [
          SizedBox(
            width: width,
            child: _buildItem('static/images/shezhi.png', '设置', 10),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/bangzhu.png', '帮助中心', 9),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/zhanting.png', '我的展厅', 11),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/gonghai.png', '公海学院', 8),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/ogschat.png', 'OSG Chat', 7),
          ),
        ],
      ),
    );
  }
}

class Task extends StatelessWidget {
  final double width;
  const Task({super.key, required this.width});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          SizedBox(
            width: width,
            child: _buildItem('static/images/userrenwu.png', '邀请好友', 12),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/renwu.png', '我的任务', 5),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/fabu.png', '发布任务', 6),
          ),
        ],
      ),
    );
  }
}

class functionality extends StatelessWidget {
  final double width;
  const functionality({super.key, required this.width});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          SizedBox(
            width: width,
            child: _buildItem('static/images/renzheng.png', '实名认证', 1),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/ogsrenz.png', 'OSG认证', 2),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/zhibo.png', '开启直播', 3),
          ),
          SizedBox(
            width: width,
            child: _buildItem('static/images/huili.png', '汇率换算', 4),
          ),
        ],
      ),
    );
  }
}

dai(){
   Get.dialog(
                AlertDialog(
                  backgroundColor: Colors.transparent,
                  content: Stack(
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(15.0),
                        child: Image.asset(
                          'static/qidai.png',
                          width: 250,
                          fit: BoxFit.fitWidth,
                        ),
                      ),
                    ],
                  ),
                ),
              );
}

Widget _buildItem(String imagePath, String text, int type) {
  return GestureDetector(
    onTap: () async{
      if (type == 4) {
        Get.toNamed('/rate');
      } else if (type == 1) {
        Get.toNamed('/certification');
      } else if (type == 3) {
        Get.toNamed('/live_seeting');
      }else if (type == 7) {
         dai();
      }else if (type == 8) {
         dai();
        
      }else if (type == 10) {

        final res = await  Get.toNamed('/setting');
        if(res){
          getuser();
        }


      } else if (type == 9) {
        Get.toNamed('/help');
      } else if (type == 11) { 
        Get.toNamed('/web_webview?url=https://test.cg-online.cn/ZGYH/index.html');


      } else if (type == 2) {
        Get.toNamed('/osg_authentication');
      } else if (type == 5) {
        Get.toNamed('/users/user/my_task');
      } else if (type == 6) {
        Get.toNamed('/talk/task_list');
      } else if (type == 12) {
        Get.toNamed('/invitation');
      }
    },
    child: Padding(
      padding: const EdgeInsets.symmetric(horizontal: 0.0, vertical: 8.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            imagePath,
            width: 30,
            fit: BoxFit.fitWidth,
          ),
          Text(
            text,
            style: TextStyle(fontSize: 12, color: Color(0xff333333)),
          ),
        ],
      ),
    ),
  );
}

class userTopInfo extends StatelessWidget {
  const userTopInfo({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Row(
          children: [
            // 获赞部分
            Row(
              children: [
                Text('${info['up_count']}'),
                SizedBox(width: 8),
                Text(
                  '获赞',
                  style: TextStyle(color: Color(0xff999999),fontSize: 16),
                ),
                SizedBox(width: 8),
                Text('|'),
              ],
            ),
            SizedBox(width: 20),
            // 关注部分
            Row(
              children: [
                Text('${info['follow_count']}'),
                SizedBox(width: 8),
                GestureDetector(
                  onTap: () {
                    Get.toNamed('/follow');
                  },
                  child: Text(
                    '关注',
                    style: TextStyle(color: Color(0xff999999),fontSize: 16),
                  ),
                ),
                SizedBox(width: 8),
                Text('|'),
              ],
            ),
            // 粉丝部分
            SizedBox(width: 20),

            Row(
              children: [
                Text('${info['fans_count']}'),
                SizedBox(width: 8),
                Text(
                  '粉丝',
                   style: TextStyle(color: Color(0xff999999),fontSize: 16),
                ),
              ],
            ),
          ],
        ));
  }
}
